package cn.iocoder.yudao.module.wine.service.premiumrecord;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;

import cn.iocoder.yudao.module.wine.controller.admin.premiumrecord.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.premiumrecord.PremiumRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.premiumrecord.PremiumRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 保费记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Slf4j
@Validated
public class PremiumRecordServiceImpl implements PremiumRecordService {

    @Resource
    private PremiumRecordMapper premiumRecordMapper;

    @Override
    public Long createPremiumRecord(PremiumRecordSaveReqVO createReqVO) {
        // 插入
        PremiumRecordDO premiumRecord = BeanUtils.toBean(createReqVO, PremiumRecordDO.class);
        premiumRecordMapper.insert(premiumRecord);
        // 返回
        return premiumRecord.getId();
    }

    @Override
    public void updatePremiumRecord(PremiumRecordSaveReqVO updateReqVO) {
        // 校验存在
        validatePremiumRecordExists(updateReqVO.getId());
        // 更新
        PremiumRecordDO updateObj = BeanUtils.toBean(updateReqVO, PremiumRecordDO.class);
        updateObj.setTradeTime(LocalDateTime.now());
        log.info("查看保费记录修改情况:{}",updateObj);
        premiumRecordMapper.updateById(updateObj);
    }

    @Override
    public void deletePremiumRecord(Long id) {
        // 校验存在
        validatePremiumRecordExists(id);
        // 删除
        premiumRecordMapper.deleteById(id);
    }

    private void validatePremiumRecordExists(Long id) {
        if (premiumRecordMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public PremiumRecordDO getPremiumRecord(Long id) {
        return premiumRecordMapper.selectById(id);
    }

    @Override
    public PageResult<PremiumRecordRespVO> getPremiumRecordPage(PremiumRecordPageReqVO pageReqVO) {
        return premiumRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public PremiumRecordDO getPremiumByOrder(Long userId, Long orderId) {
        return premiumRecordMapper.selectOne(new LambdaQueryWrapperX<PremiumRecordDO>()
                .eq(PremiumRecordDO::getOrderId,orderId)
                .eq(PremiumRecordDO::getUserId,userId));

    }

    @Override
    public PremiumRecordDO getPremiumByTradeNo(String tradeNo) {

        return premiumRecordMapper.selectOne(new LambdaQueryWrapperX<PremiumRecordDO>()
                .eq(PremiumRecordDO::getTradeNo,tradeNo));
    }

    @Override
    public void updateStatus(PremiumRecordDO premiumByOrder) {
        premiumRecordMapper.updateById(premiumByOrder);
    }

}
